package OnlineShopping;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class User {
    private String userId;
    private String username;
    private String password;
    private String email;
    private String phone;
    private String registrationDate;
    private String userLevel;
    private double totalSpent;
    private HistoryRecord history;
    // Constructor
    public User(String userId, String username, String password, String email, String phone,
                String registrationDate, String userLevel, double totalSpent,HistoryRecord history) {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.email = email;
        this.phone = phone;
        this.registrationDate = registrationDate;
        this.userLevel = userLevel;
        this.totalSpent = totalSpent;
        this.history = new HistoryRecord(username);
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getRegistrationDate() {
        return registrationDate;
    }

    public void setRegistrationDate(String registrationDate) {
        this.registrationDate = registrationDate;
    }

    public String getUserLevel() {
        return userLevel;
    }

    public void setUserLevel(String userLevel) {
        this.userLevel = userLevel;
    }

    public double getTotalSpent() {
        return totalSpent;
    }

    public void setTotalSpent(double totalSpent) {
        this.totalSpent = totalSpent;
    }

    public boolean login(String username, String password) {
        // 读取 Customer.xlsx 文件并验证用户名和密码
        try (FileInputStream fis = new FileInputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Customer.xlsx");
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                Cell usernameCell = row.getCell(1); // 假设用户名在第二列
                Cell passwordCell = row.getCell(7); // 假设密码在第三列

                if (usernameCell.getStringCellValue().equals(username) &&
                        passwordCell.getStringCellValue().equals(password)) {
                    this.userId = row.getCell(0).getStringCellValue(); // 假设用户ID在第一列
                    this.username = username;
                    this.password = password;
                    // 读取其他信息
                    return true; // 登录成功
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false; // 登录失败
    }

    public void register(String username, String password, String email, String phone) {
        // 注册新用户，并将其写入 Customer.xlsx 文件
        try (FileInputStream fis = new FileInputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Customer.xlsx");
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(0);
            int lastRowNum = sheet.getLastRowNum();
            Row newRow = sheet.createRow(lastRowNum + 1);
            newRow.createCell(0).setCellValue(String.valueOf(lastRowNum - 1)); // 用户ID
            newRow.createCell(1).setCellValue(username);
            newRow.createCell(2).setCellValue("铜");// 默认用户级别
            newRow.createCell(3).setCellValue("2023-01-01");// 注册时间，示例
            newRow.createCell(4).setCellValue(0);// 总消费金额
            newRow.createCell(5).setCellValue(phone);
            newRow.createCell(6).setCellValue(email);
            newRow.createCell(7).setCellValue(password);

            try (FileOutputStream fos = new FileOutputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Customer.xlsx")) {
                workbook.write(fos);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void editPassword(String newPassword) {
        // 更新密码到 Excel 文件
        this.password = newPassword;

        try (FileInputStream fis = new FileInputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Customer.xlsx");
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                Cell idCell = row.getCell(0); // 假设用户ID在第一列

                // 获取用户ID的字符串值
                String userIdValue = getCellStringValue(idCell);

                if (userIdValue.equals(this.userId)) {
                    Cell passwordCell = row.getCell(7); // 假设密码在第八列

                    if (passwordCell != null) {
                        passwordCell.setCellValue(newPassword); // 更新密码
                    }
                    break;
                }
            }

            // 写入更改
            try (FileOutputStream fos = new FileOutputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Customer.xlsx")) {
                workbook.write(fos);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 添加购物记录
    public void addShoppingRecord(String record) {
        history.addRecord(record);
    }

    // 查看购物历史
    public void viewShoppingHistory() {
        history.viewHistory(userId);
    }

    //重置密码
    public void resetPassword(String username, String email) {
        boolean userFound = false;

        try (FileInputStream fis = new FileInputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Customer.xlsx");
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(0);

            for (Row row : sheet) {
                String usernameValue = getCellStringValue(row.getCell(1)); // 假设用户名在第二列
                String emailValue = getCellStringValue(row.getCell(6)); // 假设邮箱在第七列

                // 验证用户名和邮箱
                if (usernameValue.equals(username) && emailValue.equals(email)) {
                    userFound = true;
                    // 生成新密码
                    String newPassword = "newPassword"; // 您可以根据需求生成随机密码
                    row.getCell(7).setCellValue(newPassword); // 更新密码
                    System.out.println("密码重置成功，新密码是: " + newPassword);
                    break;
                }
            }

            if (!userFound) {
                System.out.println("未找到用户或邮箱不匹配。");
            }

            // 写回文件
            try (FileOutputStream fos = new FileOutputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Customer.xlsx")) {
                workbook.write(fos);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // 辅助方法，获取单元格字符串值
    private String getCellStringValue(Cell cell) {
        if (cell == null) {
            return ""; // 如果单元格为空，返回空字符串
        }
        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                // 根据具体需求，您可以选择将数字转化为字符串
                // 这里假设用户名和邮箱是字符串，所以将数字转为字符串
                return String.valueOf(cell.getNumericCellValue());
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case FORMULA:
                // 如果是公式类型，您可能需要进一步处理
                return cell.getCellFormula(); // 或者返回公式的计算值
            default:
                return ""; // 对于其他类型，返回空字符串
        }
    }
    public void logout() {
        // 退出登录逻辑
        System.out.println("用户 " + username + " 已成功退出登录。");
    }
}
